﻿Imports System
Imports System.IO
Imports System.Data.SqlServerCe
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.Common
Imports System.Reflection

Public Class frmReplaceWeld

    Sub initScreen()
        loadRejectWeld()

        pnlReplaceWeldEx.Visible = False
        btnReplaceWeldReplace.Enabled = True

        txtReplacementBarcode.Text = ""
        lblRejectedWeldId.Text = ""
        lblRejectedBarcode.Text = ""
    End Sub

    Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
        _FROM_SCREEN_ID = _SCREEN_ID
        _SCREEN_ID = SCREEN_PROJECT

        frmProject.Show()
        frmProject.initScreen()
        Me.Hide()
    End Sub

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        frmMenu.Show()
        frmMenu.initScreen()
        Me.Hide()
    End Sub

    Private Sub btnReplaceWeldReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplaceWeldReplace.Click
        Dim sic As ListView.SelectedIndexCollection

        sic = lvReplaceWeld.SelectedIndices()
        If sic.Count <> 0 Then
            Dim weldLogId As String = lvReplaceWeld.Items(sic.Item(0)).Tag
            Dim barcode As String = lvReplaceWeld.Items(sic.Item(0)).SubItems(2).Text
            Dim coax As String = lvReplaceWeld.Items(sic.Item(0)).SubItems(6).Text

            lblRejectedWeldId.Text = weldLogId
            lblRejectedBarcode.Text = barcode

            pnlReplaceWeldEx.Visible = True
            btnReplaceWeldReplace.Enabled = False

            pnlReplaceWeldEx.Location = New System.Drawing.Point(16, 109)
            pnlReplaceWeldEx.Size = New System.Drawing.Size(202, 226)
        Else
            MsgBox("No record is selected.", MsgBoxStyle.Exclamation, "EQAM")
        End If
    End Sub

    Private Sub btnReplaceWeldClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplaceWeldClose.Click
        initScreen()
    End Sub

    Private Sub btnReplaceWeldSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplaceWeldSave.Click
        Dim returnExecute As Boolean = False

        If (txtReplacementBarcode.Text = "") Then
            MsgBox("Barcode cannot be blank.", MsgBoxStyle.Information, "EQAM")
            Return
        End If

        If (txtCouponBarcode.Text = "") Then
            MsgBox("Coupon Barcode cannot be blank.", MsgBoxStyle.Information, "EQAM")
            Return
        End If
        'Dim dtRejectedWeld As New DataTable
        'dtRejectedWeld = findWeldLogs(Nothing, lblRejectedBarcode.Text, Nothing, WELDLOG_STATUS_REJECTED, Nothing, Globals.COUPON_TYPE_IN, True, _USER_ID)

        'If dtRejectedWeld.Rows.Count = 0 Then
        'MsgBox(lblRejectedBarcode.Text & " is not rejected weld.", MsgBoxStyle.Information, "EQAM")
        'Return
        'End If

        If (txtReplacementBarcode.Text <> "" And checkBarcodeDuplicationForWeldlogDetail(txtReplacementBarcode.Text, Nothing)) Then
            MsgBox(txtReplacementBarcode.Text & " alraedy exists.", MsgBoxStyle.Information, "EQAM")
            Return
        End If

        Dim couponCode As String = getCouponCodeByBarcode(txtCouponBarcode.Text)

        If (couponCode = "") Then
            MsgBox("Coupon Barcode is invalid.", MsgBoxStyle.Information, "EQAM")
            Return
        End If

        Dim seq As Integer = Nothing
        'If (txtWeldInputSeq.Text <> "") Then
        'Try
        'seq = Convert.ToDecimal(txtWeldInputSeq.Text)
        'Catch ex As Exception
        'seq
        'End Try
        'End If

        If (seq = Nothing) Then
            seq = getWeldLogDetailMaxSeq(couponCode)
        End If

        returnExecute = saveRejectedWeldNo(_PROJECT_ID, lblRejectedWeldId.Text, lblRejectedBarcode.Text, txtReplacementBarcode.Text, couponCode, seq)

        If returnExecute = True Then
            MsgBox("Replace Weld Successfully.", MsgBoxStyle.Information, "E-QAM")
            initScreen()
        End If
    End Sub

    Private Sub btnReplaceWeldSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplaceWeldSearch.Click
        loadRejectWeld()
    End Sub

    Sub loadRejectWeld()
        Dim dtResult As New DataTable
        Dim lstView As New ListViewItem
        Dim barcode As String = txtReplaceWeldBarcode.Text
        Dim coupon As String = cboReplaceWeldCoupon.Text
        Dim weldInputDate As String = cboReplaceWeldDate.Text

        dtResult = findWeldLogs(coupon, barcode, weldInputDate, WELDLOG_STATUS_REJECTED, Nothing, Globals.COUPON_TYPE_IN, True, Nothing, _PROJECT_ID)

        Me.lvReplaceWeld.Items.Clear()
        If dtResult.Rows.Count > 0 Then
            For Each dataRow As DataRow In dtResult.Rows
                lstView = Me.lvReplaceWeld.Items.Add(New ListViewItem(dataRow.Item("COUPON_CODE").ToString))
                lstView.SubItems.Add(dataRow.Item("SEQ").ToString)
                Dim barcodeString As String = ""

                If (dataRow.Item("BARCODE") <> Nothing) Then
                    barcodeString = dataRow.Item("BARCODE")
                End If

                If (dataRow.Item("BARCODE_REF") <> Nothing) Then
                    If (barcodeString <> "") Then
                        barcodeString = barcodeString & "/" & dataRow.Item("BARCODE_REF")
                    Else
                        barcodeString = dataRow.Item("BARCODE_REF")
                    End If

                End If
                lstView.SubItems.Add(barcodeString)
                lstView.SubItems.Add(dataRow.Item("MATERIAL").ToString & "-" & dataRow.Item("SIZE").ToString & "-" & dataRow.Item("THICKNESS").ToString)
                lstView.SubItems.Add(dataRow.Item("WELD_DATE").ToString & "-" & dataRow.Item("WELD_TIME").ToString)
                lstView.SubItems.Add(dataRow.Item("SYSTEM_TYPE_NAME").ToString)
                lstView.SubItems.Add(dataRow.Item("COAX").ToString)
                lstView.SubItems.Add(dataRow.Item("REJECT_REASON").ToString)
                lstView.Tag = dataRow.Item("WELDLOG_DETAIL_ID")
            Next            
        End If        
    End Sub
End Class